package com.enjoy.xbase.plug;

import android.util.Log;

import com.alibaba.fastjson.JSON;

import java.lang.reflect.Type;


public class CacheData<T> {
    private T data;
    private long cacheTime;//存放时间
    private long outTime = -1;
    private String cacheTag;//缓存数据标识

    public static Type getClassType(Class<?> clazz) {
        // 返回表示此类型实际类型参数的 Type 对象的数组
        Type[] actualTypeArguments = clazz.getTypeParameters();
        return actualTypeArguments[0];

    }

    public long getCacheTime() {
        return cacheTime;
    }

    public void setCacheTime(long cacheTime) {
        this.cacheTime = cacheTime;
    }

    public String getCacheTag() {
        return cacheTag;
    }

    public void setCacheTag(String cacheTag) {
        this.cacheTag = cacheTag;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public long getOutTime() {
        return outTime;
    }

    public void setOutTime(long outTime) {
        this.outTime = outTime;
    }

    public void setContent(String content) {
        Type clazz = getClassType(getClass());
        if (clazz.getClass().getName().equals(String.class.getName())) {
            data = (T) content;
        } else {
            try {
                data = JSON.parseObject(content, clazz);
                if (data == null) {
                    throw new RuntimeException("gson解析异常");
                }
            } catch (Exception e) {
                Log.e("", "gson解析出错");
                throw new RuntimeException("gson解析异常");
            }
        }
    }
}
